package com.yuncool.jx3robot.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yuncool.jx3robot.mirai.config.MiraiConfig;
import com.yuncool.jx3robot.mirai.entily.QueryData;
import com.yuncool.jx3robot.service.JxQueryApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yuncool
 */
@Service
@Slf4j
public class JxQueryApiServiceImpl implements JxQueryApiService {
    private static final String SERVER_URL = "https://jx3api.com/app";
    @Autowired
    MiraiConfig miraiConfig;

    @Override
    public JSONObject getDaily(String server) {
        Map<String, Object> params = new HashMap<>();
        params.put("server", server);
        String rep = HttpUtil.get(SERVER_URL + "/daily", params);
        log.info("rep:{}", rep);
        QueryData queryData = JSONObject.parseObject(rep, QueryData.class);
        if (queryData.getCode() != 200) {
            return null;
        } else {
            return queryData.getData();
        }
    }

    @Override
    public JSONObject getMacro(String xf) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", xf);
        String rep = HttpUtil.get(SERVER_URL + "/macro", params);
        log.info("rep:{}", rep);
        QueryData queryData = JSONObject.parseObject(rep, QueryData.class);
        if (queryData.getCode() != 200) {
            return null;
        } else {
            return queryData.getData();
        }
    }

    @Override
    public JSONObject getServerStatus(String server) {
        Map<String, Object> params = new HashMap<>();
        params.put("server", server);
        String rep = HttpUtil.get(SERVER_URL + "/check", params);
        log.info("rep:{}", rep);
        QueryData queryData = JSONObject.parseObject(rep, QueryData.class);
        if (queryData.getCode() != 200) {
            return null;
        } else {
            return queryData.getData();
        }
    }

    @Override
    public JSONObject getSand(String server) {
        Map<String, Object> params = new HashMap<>();
        params.put("server", server);
        String rep = HttpUtil.get(SERVER_URL + "/sand", params);
        log.info("rep:{}", rep);
        QueryData queryData = JSONObject.parseObject(rep, QueryData.class);
        if (queryData.getCode() != 200) {
            return null;
        } else {
            return queryData.getData();
        }
    }

    @Override
    public JSONObject getEquipment(String s) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", s);
        String rep = HttpUtil.get(SERVER_URL + "/equip", params);
        log.info("rep:{}", rep);
        QueryData queryData = JSONObject.parseObject(rep, QueryData.class);
        if (queryData.getCode() != 200) {
            return null;
        } else {
            return queryData.getData();
        }
    }

    @Override
    public JSONObject getMethod(String text) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", text);
        String rep = HttpUtil.get(SERVER_URL + "/method", params);
        log.info("rep:{}", rep);
        QueryData queryData = JSONObject.parseObject(rep, QueryData.class);
        if (queryData.getCode() != 200) {
            return null;
        } else {
            return queryData.getData();
        }
    }

    @Override
    public JSONObject getDog() {
        String rep = HttpUtil.get("https://jx3api.com/extend/diary");
        log.info("rep:{}", rep);
        QueryData queryData = JSONObject.parseObject(rep, QueryData.class);
        if (queryData.getCode() != 200) {
            return null;
        } else {
            return queryData.getData();
        }
    }

    @Override
    public JSONObject getRandom() {
        String rep = HttpUtil.get(SERVER_URL + "/random");
        log.info("rep:{}", rep);
        QueryData queryData = JSONObject.parseObject(rep, QueryData.class);
        if (queryData.getCode() != 200) {
            return null;
        } else {
            return queryData.getData();
        }
    }

    @Override
    public JSONObject getHeighten(String s) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", s);
        String rep = HttpUtil.get(SERVER_URL + "/strengthen", params);
        log.info("rep:{}", rep);
        QueryData queryData = JSONObject.parseObject(rep, QueryData.class);
        if (queryData.getCode() != 200) {
            return null;
        } else {
            return queryData.getData();
        }
    }

    @Override
    public JSONObject getServer(String server) {
        String rep = HttpUtil.get(SERVER_URL + "/server?name=" + server);
        log.info("rep:{}", rep);
        QueryData queryData = JSONObject.parseObject(rep, QueryData.class);
        if (queryData.getCode() != 200) {
            return null;
        } else {
            return queryData.getData();
        }

    }

    @Override
    public JSONObject getMoney(String server) {
        Map<String, Object> params = new HashMap<>();
        params.put("server", server);
        String rep = HttpUtil.get(SERVER_URL + "/gold", params);
        log.info("rep:{}", rep);
        QueryData queryData = JSONObject.parseObject(rep, QueryData.class);
        if (queryData.getCode() != 200) {
            return null;
        } else {
            return queryData.getData();
        }
    }

    @Override
    public JSONArray getSerendipity(String server, String text) {
        String[] s = text.split(" ");
        Map<String, Object> params = new HashMap<>();
        params.put("server", server);
        if (s.length == 1) {
            //这个时候需要请求两次
            params.put("name", s[0]);
            String rep = HttpUtil.get(SERVER_URL + "/serendipity", params);
            JSONObject jsonObject = JSONObject.parseObject(rep);
            log.info("rep:{}", rep);
            if (jsonObject.getInteger("code") == 200) {
                return jsonObject.getJSONArray("data");
            } else {
                params.remove("name");
                params.put("serendipity", s[0]);
            }
        }
        if (s.length == 2) {
            //这个时候必定是两个参数
            params.put("name", s[0]);
            params.put("serendipity", s[1]);
        }
        String rep = HttpUtil.get(SERVER_URL + "/serendipity", params);
        log.info("rep:{}", rep);
        JSONObject jsonObject = JSONObject.parseObject(rep);
        if (jsonObject.getInteger("code") != 200) {
            return null;
        } else {
            return jsonObject.getJSONArray("data");
        }
    }

    @Override
    public JSONObject getAchievement(String key) {
        HttpRequest get = HttpUtil.createGet("https://helper.jx3box.com/api/achievement/search?page=1&keyword=" + key);
        get.header("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36");
        HttpResponse execute = get.execute();
        JSONObject rep = JSONObject.parseObject(execute.body());
        log.info("rep:{}", rep);
        return rep;
    }

    @Override
    public JSONObject getAnnounce() {
        String rep = HttpUtil.get(SERVER_URL + "/announce");
        log.info("rep:{}", rep);
        JSONObject queryData = JSONObject.parseObject(rep);
        JSONArray data = queryData.getJSONArray("data");
        return data.getJSONObject(0);
    }

    @Override
    public JSONObject getFlower(String server, String text) {
        String[] s = text.split(" ");
        Map<String, Object> params = new HashMap<>();
        params.put("server", server);
        if (s.length == 1) {
            //这个时候需要请求两次
            params.put("flower", s[0]);
            String rep = HttpUtil.get(SERVER_URL + "/flower", params);
            QueryData data = JSONObject.parseObject(rep, QueryData.class);
            log.info("rep:{}", rep);
            if (data.getCode() != 403) {
                return data.getData();
            } else {
                params.remove("flower");
                params.put("map", s[0]);
            }
        }
        if (s.length == 2) {
            //这个时候必定是两个参数
            params.put("flower", s[0]);
            params.put("map", s[1]);
        }
        String rep = HttpUtil.get(SERVER_URL + "/flower", params);
        log.info("rep:{}", rep);
        QueryData queryData = JSONObject.parseObject(rep, QueryData.class);
        if (queryData.getCode() != 200) {
            return null;
        } else {
            return queryData.getData();
        }
    }

    @Override
    public JSONObject getSeniority(String server, String mp, int i) {
        Map<String, Object> params = new HashMap<>();
        if (i == 1) {
            params.put("server", server);
        }
        String[] s = mp.split(" ");
        if (s.length == 1 && !StrUtil.isBlank(s[0])) {
            params.put("sect", s[0]);
        }
        String rep = HttpUtil.get(SERVER_URL + "/seniority", params);
        log.info("rep:{}", rep);
        JSONObject jsonObject = JSONObject.parseObject(rep);
        if (jsonObject.getInteger("code") != 200) {
            return null;
        }
        return jsonObject;
    }
}
